﻿var scriptCount = 0;
// API Utilities
var API = function(){
    // Dynamiclly Load CSS, Scripts, and Html
    function loadscriptfile(uri, uriType, id){
        if (uriType=="js"){ //if filename is a external JavaScript file
            var fileref=document.createElement('script')
            fileref.setAttribute("type","text/javascript")
            fileref.setAttribute("src", uri)
            scriptCount = scriptCount + 1;
            if (fileref.readyState){  //IE
                fileref.onreadystatechange = function(){
                    if (fileref.readyState == "loaded" || fileref.readyState == "complete"){
                            fileref.onreadystatechange = null;
                            scriptCount = scriptCount - 1;
                    }
                };
            } 
            else 
            {  // Firefox, Safari, Chrome, and Opera
                fileref.onload = function(){
                    scriptCount = scriptCount - 1;
                };
            }
            	
        }
        else if (uriType=="css"){ //if filename is an external CSS file
            var fileref=document.createElement("link")
            fileref.setAttribute("rel", "stylesheet")
            fileref.setAttribute("type", "text/css")
            fileref.setAttribute("href", uri)
        }

        if (typeof fileref!="undefined"){
            document.getElementsByTagName("head")[0].appendChild(fileref)
        }
    }
    
    function init(){
        // Load the Application Service Script
        loadscriptfile("http://localhost:49000/scripts/calculator.js", "js");
    
    }
    
    return{
        Initilize: init,
        LoadScript: loadscriptfile
    }

}();

// Load support objects in to page
API.LoadScript("http://localhost:49000/styles/master.css", "css");
API.LoadScript("http://localhost:49000/scripts/json2.js", "js");
API.LoadScript("http://localhost:49000/scripts/jquery-1.3.1.js", "js");

// wait for scripts to load
var timerId = setInterval(function(){
	if ( scriptCount < 1 ) {
		clearInterval( timerId );
		API.Initilize();
	}
}, 50);
